import wx
import math
import VarGlobales

class DrawZone():
    
    def __init__(self,panel,statusbar):
        self.Panel_Canvas = panel
        self.statusbar = statusbar
        self.Px = 450
        self.Py = 300
        self.Orientation = 0
        self.Touch = 1  #1 : contacto 0 : no contacto 

        
        self.InitDrawing()
        self.initBuffer()
        self.eventos()

        
    def InitDrawing(self):
        imageFile = 'Moo.ico'
        #self.imgrobot = wx.Image(imageFile, wx.BITMAP_TYPE_ANY)
        #a = self.imgrobot.ConvertToBitmap()
        #self.img = wx.StaticBitmap(self.Panel_Canvas, -1, a, (self.Px-5,self.Py-5), (a.GetWidth(), a.GetHeight()))
        self.ColorPen = (0,0,0)
        self.ColourInit = self.Panel_Canvas.GetBackgroundColour()
        self.ColorSurface = self.ColourInit
        #self.Panel_Canvas.SetBackgroundColour(self.surface)
        #self.Panel_Canvas.Refresh()
        self.listeners =  []
        self.thickness = 1
        self.SetColour("Black")
        self.lines = []
        
        
    def eventos(self):
        wx.EVT_SIZE(self.Panel_Canvas, self.onSize)
        wx.EVT_IDLE(self.Panel_Canvas, self.onIdle)
  
        # and the refresh event
        wx.EVT_PAINT(self.Panel_Canvas, self.onPaint)
        wx.EVT_ICONIZE(VarGlobales.window, self.onMinimizer)

    

    def Notify(self):
        for other in self.listeners:
            other.Update(self.colour, self.thickness)

    def GetLinesData(self):
        return self.lines[:]
    
    def GetColorPen(self):
        return self.ColorPen
    
    def GetColorSurface(self):
        return self.ColorSurface
    
    def GetWidthPen(self):
        return self.thickness
    
    def GetPosX(self):
        return self.Px
    
    def GetPosY(self):
        return self.Py
    
    def GetOrientation(self):
        return self.Orientation
    
    def GetTouch(self):
        return self.Touch
    
    def SetColour(self, colour):
        """Set a new colour and make a matching pen"""
        self.colour = colour
        self.pen = wx.Pen(self.colour, self.thickness, wx.SOLID)
        self.Notify()
    
    def SetColorPen(self,color):
        #self.surface.SetPen(wx.Pen(color, self.WidthPen))
        #self.ColorPen = color
        self.SetColour(color)
        
    def SetLinesData(self, lines):
        self.lines = lines[:]
        self.InitBuffer()
        self.Refresh()
    
    def SetWidthPen(self,width):
        self.thickness = width
        self.pen = wx.Pen(self.colour, self.thickness, wx.SOLID)
        self.Notify()
        
    def SetColorSurface(self,color):
        self.Panel_Canvas.SetBackgroundColour(color)
        #self.surface.SetBackground(wx.Brush(self.Panel_Canvas.GetBackgroundColour()))
        self.ColorSurface = color
        self.initBuffer()
        self.Panel_Canvas.Refresh()
    
    def SetTouch(self,val):
        self.Touch = val
        
    def RestartApp(self):
        self.defaultVal()
        self.RestartPos()
    
    def RestartPos(self):
        self.Px = 450
        self.Py = 300
        self.Orientation = 0
        self.lines = []
        dc = wx.ClientDC(self.Panel_Canvas)
        dc.Clear()
        #self.img.SetPosition((self.Px-10,self.Py-10))
        self.initBuffer()
        self.Panel_Canvas.Refresh()
        self.statusbar.SetStatusText("Direccion Grados: 0.00",4) 
        self.statusbar.SetStatusText("Posicion X: "+str(round(self.Px,2)), 2)      #posX
        self.statusbar.SetStatusText("Posicion Y: "+str(round(self.Py,2)), 3)      #posY
        
        
    def defaultVal(self):
        self.SetColorSurface(self.ColourInit)
        self.SetColorPen("black")
        self.SetWidthPen(1)
        self.SetTouch(1)
        
    def TurnRight(self,grade):
        #centro = wx.Point( self.imgrobot.GetWidth()/2, self.imgrobot.GetHeight()/2 )
        #a=self.imgrobot.Rotate((math.radians(self.Orientation + grade) % 360),centro)
        #a = a.ConvertToBitmap()
        #self.img.
        #self.img = wx.StaticBitmap(self.Panel_Canvas, -1, a, (self.Px-10,self.Py-10), (a.GetWidth(), a.GetHeight()))
        self.Orientation = (self.Orientation + grade) % 360
        self.statusbar.SetStatusText("Direccion Grados: "+str(round((0.00 if ((360.00-self.Orientation)==360) else (360.00-self.Orientation)),2)), 4)      #Dir
        
    def TurnLeft(self,grade):
        #centro = wx.Point( self.imgrobot.GetWidth()/2, self.imgrobot.GetHeight()/2 )
        #a=self.imgrobot.Rotate((math.radians(self.Orientation + grade) % 360),centro)
        #a = a.ConvertToBitmap()
        #self.img = wx.StaticBitmap(self.Panel_Canvas, -1, a, (self.Px-10,self.Py-10), (a.GetWidth(), a.GetHeight()))
        self.Orientation = (self.Orientation - grade) % 360
        self.statusbar.SetStatusText("Direccion Grados: "+str(round((0.00 if ((360.00-self.Orientation)==360) else (360.00-self.Orientation)),2)), 4)      #Dir
        
    def Move(self, dis, dir):
        curLine = []
        self.previousPosition = (self.Px,self.Py)
        #dc = wx.BufferedDC(wx.ClientDC(self.Panel_Canvas),self.buffer)
        dc = wx.ClientDC(self.Panel_Canvas)
        dc.BeginDrawing()
        dc.SetPen(self.pen)
        if dir == 0:
            xp = self.Px + (dis * math.cos(math.radians(self.Orientation)))
            yp = self.Py + (dis * math.sin(math.radians(self.Orientation)))
        else:
            xp = self.Px - (dis * math.cos(math.radians(self.Orientation)))
            yp = self.Py - (dis * math.sin(math.radians(self.Orientation)))
        
        pos = (xp,yp)
        
       # self.img.SetPosition((xp-5,yp-5))
        
        
        
        coords = (self.Px, self.Py) + pos
        curLine.append(coords)
        if self.Touch != 0:
            self.Line(dc,self.Px, self.Py, pos[0], pos[1])
            self.lines.append( (self.colour, self.thickness, curLine) )
        self.Px, self.Py = pos
        
        self.statusbar.SetStatusText("Posicion X: "+str(round(self.Px,2)), 2)      #posX
        self.statusbar.SetStatusText("Posicion Y: "+str(round(self.Py,2)), 3)      #posY
        
        
        
        
        
        #self.initBuffer()
        
        dc.EndDrawing()
        
        
           
        #self.Panel_Canvas.Refresh()
        
    def Line(self,dc,x1,y1,x2,y2):
        dc.DrawLine(x1, y1, x2, y2)

    def initBuffer(self):
        ''' Initialize the bitmap used for buffering the display. '''
        size = self.Panel_Canvas.GetSize()
        self.buffer =  wx.EmptyBitmap(size.width, size.height)
        dc = wx.BufferedDC(None, self.buffer)
        #dc = wx.ClientDC(self.Panel_Canvas)
        dc.SetBackground(wx.Brush(self.Panel_Canvas.GetBackgroundColour()))
        dc.Clear()
        self.DrawLines(dc)
        
        self.reInitBuffer = False

    # Event handlers:

    def onSize(self, event):
        ''' Called when the window is resized. We set a flag so the idle
            handler will resize the buffer. '''
        self.reInitBuffer = True

    def onIdle(self, event):
        ''' If the size was changed then resize the bitmap used for double
            buffering to match the window size.  We do it in Idle time so
            there is only one refresh after resizing is done, not lots while
            it is happening. '''
        if self.reInitBuffer:
            self.initBuffer()
            self.Panel_Canvas.Refresh(False)

    def onPaint(self, event):
        ''' Called when the window is exposed. '''
        # Create a buffered paint DC.  It will create the real
        # wx.PaintDC and then blit the bitmap to it when dc is
        # deleted.  Since we don't need to draw anything else
        # here that's all there is to it.
        #print 12212
        dc = wx.BufferedPaintDC(self.Panel_Canvas, self.buffer)
        #self.initBuffer()
        #self.Panel_Canvas.Refresh(False)
        
    def onMinimizer(self, event):
        ''' Called when the window is exposed. '''
        # Create a buffered paint DC.  It will create the real
        # wx.PaintDC and then blit the bitmap to it when dc is
        # deleted.  Since we don't need to draw anything else
        # here that's all there is to it.
        if not(event.Iconized()):  
            self.initBuffer()
        
    # Other methods
    def  DrawLines(self, dc):
        """
        Redraws all the lines that have been drawn already.
        """
        dc.BeginDrawing()
        for colour, thickness, line in self.lines:
            pen = wx.Pen(colour, thickness, wx.SOLID)
            dc.SetPen(pen)
            for coords in line:
                apply(dc.DrawLine, coords)
        dc.EndDrawing()
               
    def dibujar(self):
        self.initBuffer()
        self.Panel_Canvas.Refresh(False)